﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;
using Castle.Facilities.IBatisNetIntegration;
using Castle.Services.Transaction;

namespace Meqs.Logic.Implement
{
    [Transactional]
    [UsesAutomaticSessionCreation]
    public class ClsInfoLogic : CommonLogic<ClsInfo, int>, IClsInfoLogic
    {
        private readonly ITeachClsCauseLogic _teaClsCauLogic;
        private readonly IClassHistoryLogic _clsHistoryLogic;
        private readonly ISchoolInfoLogic _schoolInfoLogic;

        public ClsInfoLogic(ITeachClsCauseLogic teaClsCauLogic, IClassHistoryLogic clsHistoryLogic, ISchoolInfoLogic schoolInfoLogic)
        {
            _teaClsCauLogic = teaClsCauLogic;
            _clsHistoryLogic = clsHistoryLogic;
            _schoolInfoLogic = schoolInfoLogic;
        }

        #region Implementation of ICommonLogic<ClsInfo, int>

        /// <sammury>
        /// 创建对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Create(ClsInfo entity)
        {
            return CreateObject(entity);
        }

        /// <sammury>
        /// 更新对象
        /// </sammury>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(ClsInfo entity)
        {
            return UpdateObject(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public int Delete(int entityId)
        {
            return DeleteObject(entityId);
        }

        /// <summary>
        /// 删除所有对象
        /// </summary>
        /// <returns></returns>
        public int DeleteAll()
        {
            return DeleteAllObject();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityId"></param>
        /// <returns></returns>
        public ClsInfo FindById(int entityId)
        {
            return FindObjectById(entityId);
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount()
        {
            return FindObjectByCount();
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<ClsInfo> FindAll()
        {
            return FindAllObject();
        }

        /// <summary>
        /// 检索对象数量
        /// </summary>
        /// <returns></returns>
        public int FindCount(Hashtable table)
        {
            return FindObjectByCount(table);
        }

        /// <sammury>
        /// 检索对象
        /// </sammury>
        public IList<ClsInfo> FindAll(Hashtable table)
        {
            return FindObject(table);
        }

        #endregion

        /// <summary>
        /// 查询指定的Grade
        /// </summary>
        /// <param name="idList"></param>
        /// <param name="schoolMark"></param>
        /// <returns></returns>
        public IList<int> FindGradeByIdList(string idList, string schoolMark)
        {
            Hashtable ht = new Hashtable();
            if (idList.Trim() != "")
            {
                ht.Add("GradeList", "(" + idList + ")");
            }
            ht.Add("SchoolMark", schoolMark);
            return FindCustomer<int>("ClsInfo.FindGradeByIdList", ht);
        }


        /// <summary>
        /// 根据年级ID来查询班级信息
        /// </summary>
        /// <param name="gradeID"></param>
        /// <returns></returns>
        public IList<VClsInfo> FindClassInfo(Hashtable ht)
        {
            try
            {
                return FindCustomer<VClsInfo>("ClsInfo.FindClassByGradeID", ht);
            }
            catch (Exception ex)
            {
                throw;
            }
                
        }

        /// <summary>
        /// 根据年级ID, 所在学年来查询班级信息
        /// </summary>
        public IList<VClsInfo> FindClassInfoEx(Hashtable ht)
        {
            try
            {
                return FindCustomer<VClsInfo>("ClsInfo.FindClassByGradeIDEx", ht);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 根据年级ID, 所在学年和学科来查询班级信息
        /// </summary>
        public IList<VClsInfo> FindClassInfoByCauseID(Hashtable ht)
        {
            try
            {
                return FindCustomer<VClsInfo>("ClsInfo.FindClassByGradeIDAndCauseID", ht);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 根据班級ID来刪除班级信息,将班级信息置为无效，并删除联系表数据
        /// </summary>
        /// <param name="gradeID"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int DeleteByID(int RID)
        {
            try
            {
                //先将班级信息置为无效
                Hashtable htParam = new Hashtable();
                htParam.Add("RID", RID);
                htParam.Add("IsEffect", 0);
                int iResult = ExecuteUpdate("ClsInfo.SetEffect", htParam);

                if(iResult > 0)
                {
                    //删除联系表里面的数据
                    Hashtable ht = new Hashtable();
                    ht.Add("ClsID", RID);

                    _teaClsCauLogic.DeleteClsCauseTeacher(ht);
                }


                return iResult;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 根据教师ID查询教师所带的有效班级课程数
        /// </summary>
        /// <param name="teachRID"></param>
        /// <returns></returns>
        public int FindCountByTeacherID(int teachRID)
        {
            try
            {
                return FindCustomerByCount("ClsInfo.FindCountByTeacherID", teachRID);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 获取参加某次考试的所有班级信息(校考)
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        public IList<ClsInfo> FindByExamClass_S(Hashtable ht)
        {
            return FindCustomer<ClsInfo>("ClsInfo.FindByExamClass_S", ht);
        }

        /// <summary>
        /// 获取参加某次考试的所有班级信息(统考)
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        public IList<ClsInfo> FindByExamClass_E(Hashtable ht)
        {
            return FindCustomer<ClsInfo>("ClsInfo.FindByExamClass_E", ht);
        }

        /// <summary>
        /// 更换班主任信息
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int UpdateHeader(List<int> list)
        {
            try
            {
                foreach (int iTeachID in list)
                {
                    Hashtable ht = new Hashtable();
                    ht.Add("NewHeaderID", 0);
                    ht.Add("HeaderID", iTeachID);

                    UpdateHeader(ht);
                }

                return 1;
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// 更换班主任信息
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        public int UpdateHeader(Hashtable ht)
        {
            try
            {
                return ExecuteUpdate("ClsInfo.UpdateHeader", ht);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 新增班级
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int AddNewClass(ClsInfo clsInfo)
        {
            Hashtable table = new Hashtable();
            table.Add("IsEffect", 1);
            table.Add("SchoolMark", clsInfo.SchoolMark);
            table.Add("Grade", clsInfo.Grade);

            ClsInfo cls = ExecuteQueryForObject<ClsInfo>("ClsInfo.SelectMaxClassNO", table);

            if (cls == null)
                clsInfo.ClsNo = 1;
            else
                clsInfo.ClsNo = cls.ClsNo + 1;


            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", clsInfo.SchoolMark);
            List<SchoolInfo> listSchoolInfo = (List<SchoolInfo>)_schoolInfoLogic.FindAll(ht);
            if ((listSchoolInfo == null) || (listSchoolInfo.Count == 0))
                return 0;

            int res = Create(clsInfo);
            ClassHistory ch = new ClassHistory(res, res, listSchoolInfo[0].SchoolYear, clsInfo.SchoolMark);
            _clsHistoryLogic.Create(ch);

            return res;
        }

        /// <summary>
        /// 修改班级信息（除开ClsNo）
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        public int UpdateClsExceptClsNO(ClsInfo clsInfo)
        {
            try
            {
                return ExecuteUpdate("ClsInfo.UpdateClsExceptClsNO", clsInfo);
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 修改班级序号
        /// </summary>
        /// <param name="ht"></param>
        /// <returns></returns>
        [Transaction(TransactionMode.RequiresNew)]
        public int UpdateClsNO(List<ClsInfo> listClsInfo)
        {
            try
            {
                foreach (ClsInfo clsInfo in listClsInfo)
                {
                    //设置生成的班级名称
                    clsInfo.ClsName = ConvertGradeNum(clsInfo.Grade) + "(" + clsInfo.ClsNo.ToString() + ")" + "班";
                    ExecuteUpdate("ClsInfo.UpdateClsNO", clsInfo);
                }
            }
            catch
            {
                return 0;
            }

            return 1;
        }

        /// <summary>
        /// 转换年级
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string ConvertGradeNum(int num)
        {
            if (num <= 0 || num > 12) return "";
            string[] GradeStrArr = { "一年级", "二年级", "三年级", "四年级", "五年级", "六年级", "初一", "初二", "初三", "高一", "高二", "高三" };

            return GradeStrArr[num - 1];
        }

        /// <summary>
        /// 獲取與教師有關的班級
        /// </summary>
        public IList<ClsAndCauseView> GetClsInTeacher(String schoolMark, int teachID)
        { 
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", schoolMark);
            ht.Add("TeachID", teachID);
            IList<ClsAndCauseView> list = this.ExecuteQueryForList<ClsAndCauseView>("ClsInfo.GetClsInTeacher", ht);

            return list;
        }


        /// <summary>
        /// 获取班主任所在的历史班级
        /// </summary>
        /// <param name="schoolYear"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public IList<ClsInfo> SelectHisClsInfoByHeader(int schoolYear, int userId)
        {
            Hashtable ht = new Hashtable();
            ht.Add("SchoolYear",schoolYear);
            ht.Add("userID",userId);

            return FindCustomer<ClsInfo>("ClsInfo.SelectHisClsInfoByHeader", ht);
        }
    }
}